package at.horario.web.pages;

import static at.horario.Constants.PAGE_SIZE;
import at.horario.model.Horario;
import at.horario.model.PropertiesFactory;
import at.horario.service.HorarioManager;
import at.horario.web.pages.data.GenericSortableDataProvider;
import at.horario.web.property.Property;
import at.horario.web.property.editor.PropertyColumnAdapter;
import org.apache.wicket.Component;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilteredAbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.GoAndClearFilter;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.ArrayList;
import java.util.List;


@AuthorizeInstantiation({"horariolist", "admin"})
public class HorarioList extends PageTemplate {
    @SpringBean
    private HorarioManager horarioManager;


    /**
     * Provides a composite User Actions panel for the Actions column.
     *
     * @author igor
     */
    private static class UserActionsPanel extends Panel {
        public UserActionsPanel(String id, IModel model) {
            super(id);
            addEditLink(model);
            addDeleteLink(model);
            addProcessLink(model);
            addQuadroLink(model);
        }

        private void addDeleteLink(IModel model) {
            add(new Link("deleteLink", model) {
                /**
                 * Go to the Delete page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new DeleteHorarioPage(getPage(), getModel()));
                }
            });
        }

        private void addEditLink(IModel model) {
            add(new Link("editLink", model) {
                /**
                 * Go to the Edit page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new HorarioForm(getPage(), getModel()));
                }
            });
        }

        private void addQuadroLink(IModel model) {
            add(new Link("quadroLink", model) {
                /**
                 * Go to the Edit page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new QuadroForm((Horario) getModel().getObject()));
                }
            });
        }

        private void addProcessLink(IModel model) {
            add(new Link("processLink", model) {
                /**
                 * Go to the Edit page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new HorarioScheduleForm(getPage(), (Horario) getModel().getObject()));
                }
            });
        }

    }


    public HorarioList() {

//        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
//        add(feedbackPanel);
//        feedbackPanel.setVisible(false); // other pages will set this to visible
//        feedbackPanel.setEscapeMessages(false);

        // Form and button for routing horario to add a new horario
        Form form = new Form("form");
        Button button = new Button("add-horario") {
            public void onSubmit() {
                onEditHorario(new Model(new Horario()));
            }
        };
        button.setDefaultFormProcessing(false);
        form.add(button);
        add(form);

        SortableDataProvider dp = new GenericSortableDataProvider<Horario, Long>(horarioManager);

        List<Property> props = PropertiesFactory.getHorarioProperties();

        List<IColumn> cols = new ArrayList<IColumn>();

        for (int i = 0; i < props.size(); i++) {
            Property prop = props.get(i);
            cols.add(new PropertyColumnAdapter(prop));
        }

        IColumn column = new FilteredAbstractColumn(new Model(getString("actions"))) {
            // return the go-and-clear filter for the filter toolbar
            public Component getFilter(String componentId, FilterForm form) {
                return new GoAndClearFilter(componentId, form,
                        new ResourceModel("filter"), new ResourceModel("clear"));
            }

            // add the UserActionsPanel to the cell item
            public void populateItem(Item cellItem, String componentId,
                                     IModel model) {
                cellItem.add(new UserActionsPanel(componentId, model));
            }
        };


        cols.add(column);

        AjaxFallbackDefaultDataTable table = new AjaxFallbackDefaultDataTable("list", cols.toArray(new IColumn[cols.size()]), dp, PAGE_SIZE);
//        table.addTopToolbar(new NavigationToolbar(table));
//        table.addTopToolbar(new HeadersToolbar(table, dp));
//        table.addBottomToolbar(new NoRecordsToolbar(table));
//        table.

        add(table);
    }


    /**
     * Listener for the edit action
     *
     * @param horario horario to be edited
     */
    protected void onEditHorario(IModel model) {
        setResponsePage(new HorarioForm(this, model));
    }

    protected void onProcessar(Horario horario) {
        //TODO: criar HorarioScheduleForm
        //setResponsePage(new HorarioScheduleForm(this, horario));
    }

}